﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace CountdownTimer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private byte _timerValue;
        public DispatcherTimer DT;
        public byte TimerValue
        {
            get { return _timerValue; }
            set { _timerValue = value; }
        }

        public MainWindow()
        {
            InitializeComponent();
            TimerValue = 0;
            lblCurrentCount.Content = TimerValue;
            DT = new DispatcherTimer(new TimeSpan((long)Math.Pow(10, 7)), DispatcherPriority.Background, TimerTick, Dispatcher);
            DT.Stop();
        }

        public void TimerTick(object sender, EventArgs e)
        {
            if (TimerValue >= 0 && TimerValue <= 15)
            {
                lblCurrentCount.Content = TimerValue;
                TimerValue--;
            }
            else
            {
                DT.Stop();
                SwitchButtons(false);
                MessageBox.Show("Finished!");
            }
        }

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            TimerValue = 15;
            DT.Start();
            SwitchButtons(true);
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            DT.Stop();
            SwitchButtons(false);
        }

        private void SwitchButtons(bool Running)
        {
            btnStart.IsEnabled = !Running;
            btnStop.IsEnabled = Running;
        }
    }
}
